package a66weding.com.jiehuntong;
/**
 * Created by 李站旗 on 2017/2/9.
 * 作用说明:一个基本的相机预览类
 */

import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;

/** A basic Camera preview class */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    public SurfaceHolder mHolder;
    private Camera mCamera;
    private static final String TAG = "CameraPreview";
    public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }
    public void stopPreview(){
        try {if (mCamera!=null){
            mCamera.stopPreview();
            mCamera.setPreviewDisplay(null);
        }
        } catch (IOException e) {
            if (mCamera!=null){
                mCamera.release();
            }
            e.printStackTrace();
        }
    }
    public void setmCamera(Camera mCamera) {
        this.mCamera = mCamera;
    }
    public void rePreview(){
            surfaceCreated(mHolder);
    }
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            if (mCamera!=null){
                mCamera.setPreviewDisplay(holder);
                mCamera.startPreview();
            }
        } catch (IOException e) {
            mCamera.release();
            Log.d(TAG, "Error setting camera preview: " + e.getMessage());
        }
    }
   public void surfaceDestroyed(SurfaceHolder holder) {
       if (mCamera!=null){
           mCamera.stopPreview();
            mCamera.unlock();
           mCamera.release();//加上这句，就OK！
           mCamera=null;
       }
        // empty. Take care of releasing the Camera preview in your activity.空。照顾的释放相机预览在您的活动。
    }
    public void removeCallback(){
        mHolder.removeCallback(this);
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.如果您的预览，可以更改或旋转，照顾这些事件在这里。
        // Make sure to stop the preview before resizing or reformatting it.请务必在调整大小或重新格式化它之前停止预览。

        if (mHolder.getSurface() == null){
            // preview surface does not exist预览表面不存在
            return;
        }

        // stop preview before making changes在进行更改之前停止预览
        try {
            mCamera.stopPreview();
        } catch (Exception e){
            // ignore: tried to stop a non-existent preview漠视︰ 试图阻止非存在预览
        }
        // set preview size and make any resize, rotate or  reformatting changes here
        // 设置预览大小和作出任何调整大小、 旋转或重新格式化这里的变化


        // start preview with new settings以新设置开始预览
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception e){
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
    }
}
